﻿using System;
using System.Text;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using System.Net.Mail;
using System.Text.RegularExpressions;


/// <summary>
/// Summary description for Function
/// </summary>
public class Function
{
    public static bool Luhn(string Chuoi)
    {
        int CheckSum = 0;
        bool DoubleFlag = (Chuoi.Length % 2 == 0);
        int DigitValue;
        foreach (char Digit in Chuoi)
        {
            DigitValue = (int)Digit - '0';
            if (DoubleFlag)
            {
                DigitValue *= 2;
                if (DigitValue > 9)
                    DigitValue -= 9;
            }
            CheckSum += DigitValue;
            DoubleFlag = !DoubleFlag;
        }
        return CheckSum % 10 == 0;
    }

    public static string MD5(string Chuoi)
    {
        UTF32Encoding u = new UTF32Encoding();
        byte[] bytes = u.GetBytes(Chuoi);
        MD5 md = new MD5CryptoServiceProvider();
        byte[] result = md.ComputeHash(bytes);
        return Convert.ToBase64String(result);
    }

    public static byte[] SHA1(string Chuoi)
    {
        byte[] code = new byte[100];
        int i;
        for (i = 0; i < 100; i++)
            code[i] = 0;

        for (i = 0; i < Chuoi.Length; i++)
            code[i] = (byte)Chuoi[i];

        byte[] result;
        SHA1CryptoServiceProvider cryptoTransformSHA1 = new SHA1CryptoServiceProvider();
        result = cryptoTransformSHA1.ComputeHash(code);
        return result;
    }

    public static string GetSalt(int length)
    {
        byte[] randomArray = new byte[length];
        string randomString;
        RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
        rng.GetBytes(randomArray);
        randomString = Convert.ToBase64String(randomArray);
        return randomString;
    } 
    /*
    public class Mail
    {
        private System.Net.Mail.MailMessage _mail = new System.Net.Mail.MailMessage();
        public Mail(string fromName, string fromMail, string toName, string toMail, string subject, string body, bool isHtml)
        {
            this._mail.Subject = subject;
            this._mail.Body = body;
            this._mail.From = new System.Net.Mail.MailAddress(fromMail, fromName);
            this._mail.To.Add(new System.Net.Mail.MailAddress(toMail, toName));
            this._mail.IsBodyHtml = isHtml;
            this._mail.BodyEncoding = System.Text.Encoding.GetEncoding("iso-8859-1");
            this._mail.SubjectEncoding = System.Text.Encoding.GetEncoding("iso-8859-1");
        }
        public bool Send()
        {
            try
            {
                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("localhost"); // Mettre le serveur smtp ici
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.Credentials = new System.Net.NetworkCredential("daugiadientu@gmai.com", "abc12345");
                smtp.Send(this._mail);
                return true;
            }
            catch (Exception) { return false; }
        }
    }*/



    

}
